package com.signway.harmony_demo.slice;

import com.signway.harmony_demo.ResourceTable;
import com.signway.harmony_demo.devices.SelectDeviceDialog;
import com.signway.harmony_demo.slice.base.BaseAbilitySlice;
import com.signway.harmony_demo.utils.CommonData;
import com.signway.harmony_demo.utils.CommonUtil;
import com.signway.harmony_demo.utils.LogUtil;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.aafwk.content.Operation;
import ohos.agp.components.Button;
import ohos.agp.components.Component;
import ohos.agp.components.Text;
import ohos.agp.components.TextField;
import ohos.agp.utils.LayoutAlignment;
import ohos.agp.window.dialog.ToastDialog;
import ohos.data.distributed.common.KvManagerConfig;
import ohos.data.distributed.common.KvManagerFactory;
import ohos.distributedschedule.interwork.DeviceInfo;
import ohos.distributedschedule.interwork.DeviceManager;

import java.util.ArrayList;
import java.util.List;

public class MathGameAbilitySlice extends BaseAbilitySlice {
    private static final String TAG = CommonData.TAG + MathGameAbilitySlice.class.getSimpleName();
    public static final int MAX_NUM = 100;
    private static final int EXCEPTION_NUM = -1;

    private Text numberText1;
    private Text numberText2;
    private TextField answerText;
    private Button answerBtn;
    private Button nextBtn;
    private Button helpBtn;

    private int number1;
    private int number2;
    private int answer;

    private List<DeviceInfo> devices = new ArrayList<>();

    static public void startAbilitySlice() {
        AbilitySlice slice = BaseAbilitySlice.getTopAbilitySlice();
        slice.present(new MathGameAbilitySlice(), new Intent());
    }

    @Override
    protected void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_math_game);
        initView();
        setQuestion();
    }

    private void initView() {
        if (findComponentById(ResourceTable.Id_number1 ) instanceof Text) {
            numberText1 = (Text) findComponentById(ResourceTable.Id_number1);
        }
        if (findComponentById(ResourceTable.Id_number2) instanceof Text) {
            numberText2 = (Text) findComponentById(ResourceTable.Id_number2);
        }
        if (findComponentById(ResourceTable.Id_answer) instanceof TextField) {
            answerText = (TextField) findComponentById(ResourceTable.Id_answer);
        }
        if (findComponentById(ResourceTable.Id_answer_btn) instanceof Button) {
            answerBtn = (Button) findComponentById(ResourceTable.Id_answer_btn);
        }
        if (findComponentById(ResourceTable.Id_next_btn) instanceof Button) {
            nextBtn = (Button) findComponentById(ResourceTable.Id_next_btn);
        }
        if (findComponentById(ResourceTable.Id_help_btn) instanceof Button) {
            helpBtn = (Button) findComponentById(ResourceTable.Id_help_btn);
        }

        answerBtn.setClickedListener(new ButtonClick());
        nextBtn.setClickedListener(new ButtonClick());
        helpBtn.setClickedListener(new ButtonClick());
    }

    private void setQuestion() {
        number1 = CommonUtil.getRandom(MAX_NUM);
        number2 = CommonUtil.getRandom(MAX_NUM);
        LogUtil.info(TAG, "number1 = " + number1);
        LogUtil.info(TAG, "number2 = " + number2);
        numberText1.setText(number1 + "");
        numberText2.setText(number2 + "");
        answerText.setText("");
    }

    private void checkAnswer() {
        try {
            answer = Integer.parseInt(answerText.getText());
        } catch (NumberFormatException e) {
            answer = EXCEPTION_NUM;
        }
        if (answer == number1 + number2) {
            LogUtil.info(TAG, "答案正确");
            new ToastDialog(getContext()).setText("回答正确").setAlignment(LayoutAlignment.CENTER).show();
        } else {
            LogUtil.info(TAG, "答案错误");
            new ToastDialog(getContext()).setText("回答错误").setAlignment(LayoutAlignment.CENTER).show();
        }
    }

    private void getDevices() {
        LogUtil.info(TAG, "getDevices");
        if (devices.size() > 0)
            devices.clear();
        List<DeviceInfo> deviceInfoList =
                DeviceManager.getDeviceList(ohos.distributedschedule.interwork.DeviceInfo.FLAG_GET_ONLINE_DEVICE);
        LogUtil.info(TAG, "MathGameAbilitySlice deviceInfoList size is :" + deviceInfoList.size());
        devices.addAll(deviceInfoList);
        showDevicesDialog();
    }

    private void showDevicesDialog() {
        new SelectDeviceDialog(this, devices, new SelectDeviceDialog.Listener() {
            @Override
            public void onItemClickedListener(DeviceInfo deviceInfo) {
                startLocalFa(deviceInfo.getDeviceId());
                startRemoteFa(deviceInfo.getDeviceId());
            }

            @Override
            public void onHideListener() {
                startTextFa();
            }
        }).show();
    }

    private void startTextFa() {
        LogUtil.info(TAG, "startTextFa......");
        Intent intent = new Intent();
        intent.setParam(CommonData.KEY_REMOTE_DEVICE_ID, "text");
        intent.setParam(CommonData.KEY_IS_LOCAL, true);
        Operation operation = new Intent.OperationBuilder()
                .withBundleName(getBundleName())
                .withAbilityName(CommonData.ABILITY_MAIN)
                .withAction(CommonData.DRAW_PAGE)
                .build();
        intent.setOperation(operation);
        startAbility(intent);
    }

    private void startLocalFa(String deviceId) {
        LogUtil.info(TAG, "StartLocalFa......");
        Intent intent = new Intent();
        intent.setParam(CommonData.KEY_REMOTE_DEVICE_ID, deviceId);
        intent.setParam(CommonData.KEY_IS_LOCAL, true);
        Operation operation = new Intent.OperationBuilder()
                .withBundleName(getBundleName())
                .withAbilityName(CommonData.ABILITY_MAIN)
                .withAction(CommonData.DRAW_PAGE)
                .build();
        intent.setOperation(operation);
        startAbility(intent);
    }

    private void startRemoteFa(String deviceId) {
        LogUtil.info(TAG, "startRemoteFa......");
        String localDevicesId = KvManagerFactory.getInstance()
                .createKvManager(new KvManagerConfig(this))
                .getLocalDeviceInfo()
                .getId();
        Intent intent = new Intent();
        intent.setParam(CommonData.KEY_REMOTE_DEVICE_ID, localDevicesId);
        intent.setParam(CommonData.KEY_IS_LOCAL, false);
        Operation operation = new Intent.OperationBuilder()
                .withBundleName(getBundleName())
                .withAbilityName(CommonData.ABILITY_MAIN)
                .withAction(CommonData.DRAW_PAGE)
                .withFlags(Intent.FLAG_ABILITYSLICE_MULTI_DEVICE)
                .build();
        intent.setOperation(operation);
        startAbility(intent);
    }

    private class ButtonClick implements Component.ClickedListener {
        @Override
        public void onClick(Component component) {
            int btnId = component.getId();
            switch (btnId) {
                case ResourceTable.Id_answer_btn: {
                    LogUtil.info(TAG, "answer click");
                    checkAnswer();
                } break;
                case ResourceTable.Id_next_btn: {
                    LogUtil.info(TAG, "next click");
                    setQuestion();
                } break;
                case ResourceTable.Id_help_btn: {
                    LogUtil.info(TAG, "help click");
                    getDevices();
                } break;
            }
        }
    }
}
